/*
 * interrupt.c
 *
 *  Created on: 02/feb/2014
 *      Author: massimo
 */

#include <msp430.h>
#include "init.h"


volatile unsigned int contatore = 0, tempo1S = 0;

volatile unsigned char bufferRX[BUFF_DIM], Wptr = 0, Rptr = 0, endMess = 0;

// Timer1 A0 interrupt service routine
#pragma vector=TIMER1_A0_VECTOR
__interrupt void TIMER1_A0_ISR(void){
	/// l'interruzione avviene ogni 1000 ms e quindi ogni interruzione
	/// e' passato un secondo.

//	if (contatore < 9){
//		contatore++;
//	}
//	else{
		contatore = 0;
		tempo1S++;
		P4OUT ^= 0x80;
		//P6OUT ^= BIT3;
	//}
}



volatile int conteggioMinuti = 0;
#pragma vector=RTC_VECTOR
__interrupt void RealTimer(void){

	conteggioMinuti++;
	//P4OUT ^= 0x80;
	RTCIV = 0;
}


// Echo back RXed character, confirm TX buffer is ready first
#pragma vector=USCI_A1_VECTOR
__interrupt void USCI_A1_ISR(void){

  switch(__even_in_range(UCA1IV,4))
  {
  case 0:break;                             // Vector 0 - no interrupt
  case 2:                                   // Vector 2 - RXIFG
#ifdef HYPER
	while (!(UCA1IFG&UCTXIFG));             // USCI_A0 TX buffer ready?
#endif
    bufferRX[Wptr] = UCA1RXBUF;
#ifdef HYPER
    UCA1TXBUF = bufferRX[Wptr];           	// TX -> RXed character
    if (bufferRX[Wptr] == '\r'){
    	while (!(UCA1IFG&UCTXIFG));
    	UCA1TXBUF = '\n';
    	endMess = 1;
    }
#endif
    if (bufferRX[Wptr] == '\r' || bufferRX[Wptr] == '\n')
    	endMess = 1;
    Wptr++;
    Wptr &= BUFF_DIM - 1;
    break;
  case 4:break;                             // Vector 4 - TXIFG
  default: break;
  }
}


volatile unsigned char bufferRX0[BUFF_DIM], Wptr0 = 0, Rptr0 = 0, endMess0 = 0;
// Echo back RXed character, confirm TX buffer is ready first
#pragma vector=USCI_A0_VECTOR
__interrupt void USCI_A0_ISR(void){

  switch(__even_in_range(UCA0IV,4))
  {
  case 0:break;                             // Vector 0 - no interrupt
  case 2:                                   // Vector 2 - RXIFG

    bufferRX0[Wptr0] = UCA0RXBUF;
    UCA1TXBUF = bufferRX0[Wptr0];
    while (!(UCA0IFG&UCTXIFG));
    UCA0TXBUF = bufferRX0[Wptr0];
   // while (!(UCA0IFG&UCTXIFG));
   // UCA0TXBUF =  bufferRX0[Wptr0];
//#ifdef HYPER
//    UCA1TXBUF = bufferRX0[Wptr0];           	// TX -> RXed character
//    if (bufferRX0[Wptr0] == '\r'){
//    	while (!(UCA0IFG&UCTXIFG));
//    	UCA0TXBUF = '\n';
//    	endMess0 = 1;
//    }
//#endif
    //UCA0TXBUF = bufferRX0[Wptr0];
 /*   if (bufferRX0[Wptr0] == '\r' || bufferRX0[Wptr0] == '\n')
    	endMess = 1;*/
    Wptr0++;
    Wptr0 &= BUFF_DIM - 1;
    break;
  case 4:break;                             // Vector 4 - TXIFG
  default: break;
  }
}

